package string_question.leecode.test87;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/8 7:58
 */
public class Solution1 {

    // 扰乱字符串
    public boolean isScramble(String s1, String s2) {
        if (s1.length() != s2.length()) return false;
        if (s1.equals(s2)) return true;
        // 递归求解
        return helper(s1, s2);
    }

    private boolean helper(String s1, String s2) {
        if (s1.equals(s2)) return true;
        int n = s1.length();
        for (int i = 1; i < n; i++) {
            if ((helper(s1.substring(0, i), s2.substring(0, i)) &&
                    helper(s1.substring(i, n), s2.substring(i, n))) ||
                    (helper(s1.substring(0, i), s2.substring(n - i, n)) &&
                            helper(s1.substring(i, n), s2.substring(0, n - i)))) {
                return true;
            }
        }
        return false;
    }
}
